//
// Copyright (c) 2009-2011, chunquedong
//
// This file is part of ChunMap project
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
//
// History:
//   2011-09-04  Jed Young  Creation
//

using chunmapModel
using chunmapData
using gfx2
using gfx
using gfx2Imp
using web
using chunmapView
using chunmapRaster

**
** GetMap
**
const class WebMapService : WebMod
{
  const Capabilities capabilities

  new make(LayerList layers, Str serviceTile, Int srs := 4326)
  {
    capabilities = Capabilities(layers, serviceTile, srs)
  }

  override Void onService()
  {
    // Str version=request.getParameter("VERSION")
    // Str updateSequence=request.getParameter("UPDATESEQUENCE")
    Str requestStr := param("REQUEST")

    if (requestStr.equalsIgnoreCase("GetCapabilities"))
    {
      capabilities.onService
    }
    else
    {
      Str bbox := param("BBOX")
      Envelope envelop := parseEnvelop(bbox)
      Int width := param("WIDTH").toInt
      Int height := param("HEIGHT").toInt
      type := req.uri.query["FORMAT"] ?: "image/png"
      format := MimeType.fromStr(type)

      if (requestStr.equalsIgnoreCase("GetMap"))
      {
        GetMap
        {
          it.envelope = envelop
          it.size = Size(width, height)
          it.layers = capabilities.layerList
          it.format = format
        }.reponse(res)
      }
      else if (requestStr.equalsIgnoreCase("GetFeatureInfo"))
      {/*
        GetFeature getInfo=new GetFeature
        getInfo.capbilities=capabilities
        getInfo.envelop=envelop
        getInfo.formatName=format
        getInfo.height=height
        getInfo.width=width

        getInfo.service(request, response)*/
      }
      else
      {
        throw UnsupportedErr("unsupported request type: $requestStr")
      }
    }
  }

  private Envelope parseEnvelop(Str bbox)
  {
    Str[] ss := bbox.split(',')
    Float minx := (ss[0]).toFloat
    Float miny := (ss[1]).toFloat
    Float maxx := (ss[2]).toFloat
    Float maxy := (ss[3]).toFloat

    return Envelope(minx, miny, maxx, maxy)
  }

  private Str param(Str name)
  {
    req.uri.query[name]
  }
}